module utils {
	export class AlertManager extends egret.DisplayObjectContainer{

		private static _instance: utils.AlertManager;
		private alertContainer: egret.DisplayObjectContainer;
		private shelter: egret.Sprite;
		//是否正在执行动画
		private animating:boolean=false;
		//触摸背景是否隐藏
		private bgTouchHide:boolean;
		private alertView:egret.DisplayObjectContainer;

		public constructor() {
			super();
			this.initView();
			this.initEvent();
		}

		private initView(): void {
			// this.width=CoreData.instance.screenW;
			// this.height=CoreData.instance.screenH;
			this.shelter = new egret.Sprite();
			this.shelter.touchEnabled=true;
			this.shelter.graphics.beginFill(0x000000);
			this.shelter.graphics.drawRect(-CoreData.instance.screenW/2, -CoreData.instance.screenH/2, CoreData.instance.screenW, CoreData.instance.screenH);
			this.shelter.graphics.endFill();
			this.addChild(this.shelter);
		}

		private initEvent():void
		{
			pub.SignalManager.instance.addSignal(pub.SignalDefin.CLOSE_ALERT,this.hideAlert,this);
			this.shelter.addEventListener(egret.TouchEvent.TOUCH_TAP,this.shelterTouchHandle,this);
		}

		public showAlert(className:string,bgTouchHide=true):void
		{
			this.bgTouchHide=bgTouchHide;
			this.shelter.alpha=0;
			if(this.alertView){
				if(this.alertView.parent) this.removeChild(this.alertView);
				this.alertView=null;
			}
			var AlertClass:any=egret.getDefinitionByName(className);
			if(!AlertClass){
				this.alertView=new AlertView(className);
			}else{
				this.alertView=<egret.DisplayObjectContainer>new AlertClass();
			}
			this.alertView.x=-this.alertView.width/2;
			this.alertView.y=-this.alertView.height/2+6;
			this.alertView.alpha=0;
			this.addChild(this.alertView);
			this.alertContainer.addChild(this);
			this.showEffect();
		}

		public showEffect():void
		{
			this.animating=true;
			TweenMax.to(this.shelter,.5,{alpha:.7,ease:Cubic.easeOut});
			TweenMax.to(this.alertView,.2,{alpha:1,y:this.alertView.y-6,delay:.1,ease:Linear.easeOut,onComplete:()=>{
				this.animating=false;
			}});
		}

		private hideAlert():void
		{
			this.animating=true;
			TweenMax.to(this.alertView,.2,{alpha:0,y:this.alertView.y+6,ease:Linear.easeIn});
			TweenMax.to(this.shelter,.4,{alpha:0,ease:Cubic.easeIn,onComplete:()=>{
				this.animating=false;
				this.alertContainer.removeChild(this);
			}});
		}

		private shelterTouchHandle(e:egret.TouchEvent):void
		{
			if(!this.bgTouchHide) return;
			if(this.animating) return;
			this.hideAlert();
		}

		public setStageContainer(alertContainer:egret.DisplayObjectContainer):void
        {
            this.alertContainer=alertContainer;
			this.x=utils.CoreData.instance.screenW/2;
			this.y=utils.CoreData.instance.screenH/2;
        }

		

		public static get instance(): utils.AlertManager {
			if (!utils.AlertManager._instance) {
				this._instance = new utils.AlertManager();
			}
			return this._instance;
		}
	}
}